<?php
require('config.php');
require('emotion.json.php');
	$wb_id=$_REQUEST['wb_id'];
	$user_id=$_REQUEST['user_id'];
	$to_user_id=$_REQUEST['to_user_id'];
	$content=$_REQUEST['content'];
	$time=date('Y-m-d H:i:s');
	if($isEmotion==1)
    {
        for($i=0;$i<count($emotion_json);$i++)
	    {
	        	$content=str_replace($emotion_json[$i]['text'],"<img class=\"biaoqing\" src=\"../emotion/".$emotion_json[$i]['name'].".png\">",$content);
	    }
    }
	$sql="INSERT INTO ".$oto."_help_chat (`helpId`,`userId`,`acceptUserId`,`content`,`createTime`) VALUES ('$wb_id','$user_id','$to_user_id','$content','$time')";
	$result1=$db->query($sql);
	$sql="select * from `".$oto."_help_chat` where `helpId`='{$wb_id}' and userId='{$user_id}' and acceptUserId='{$to_user_id}' and content='{$content}' and createTime='{$time}'";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$help_chat_info[]=$row;//将取得的所有数据赋值给person_info数组
	}
	$sql="select * from `".$oto."_help` where `id`='{$wb_id}'";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$wb_info[]=$row;//将取得的所有数据赋值给person_info数组
	}
	if(isset($wb_info))
	{
		if($wb_info[0]['userId']==$user_id)
		{
	    	$sql="select * from ".$oto."_help_message where helpId='{$wb_id}' and userId='{$to_user_id}' and msg_type='2' and isDel='0'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$message[]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(isset($message))
			{
				$lin_msg_id=$message[0]['id'];
				$sql="UPDATE ".$oto."_help_message SET `createTime`='{$time}' where id='{$lin_msg_id}'";
				$result=$db->query($sql);
			}
			else
			{
				$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`createTime`) VALUES ('$wb_id','$to_user_id','2','$time')";
		    	$result=$db->query($sql);
			}
		}
	}
	else if($wb_id==0 || $wb_id=='0')
	{
		$cat_id=$help_chat_info[0]['id'];
		$sql="select * from ".$oto."_help_message where helpId='{$wb_id}' and userId='{$to_user_id}' and msg_type='2' and isDel='0' and comeFromUserId='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$message[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($message))
		{
			$lin_msg_id=$message[0]['id'];
			$sql="UPDATE ".$oto."_help_message SET `createTime`='{$time}',cat_id='{$cat_id}' where id='{$lin_msg_id}'";
			$result=$db->query($sql);
		}
		else
		{
			$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`cat_id`,`createTime`,`comeFromUserId`) VALUES ('$wb_id','$to_user_id','2','$cat_id','$time','$user_id')";
		    $result=$db->query($sql);
		}
	}
	if($result1)
	{
		$data[0]['pan']="true";
		$data[0]['content']=$content;
	}
	else $data[0]['pan']="false";
	echo json_encode($data);
?>